#include "McuInit.h"
#include "MS51_16K.h"

u8 Interrupt_Port_Select_Flag;
/************************************************
*    GPIO interrupt Initial setting
*************************************************/
u8	GPIO_Interrupt_Init(u8 Trig_Mode,u8 Trig_Port,u8 Trig_Pin)
{
	//if Interrupt Port was Selected,and new Interrupt Port is different from before
	if(Interrupt_Port_Select_Flag && (Trig_Port != (PICON & 0x03)))
		return 1;
	
	if((Trig_Port > PORT_0) &&(Trig_Port < PORT_3))
	{
		Interrupt_Port_Select_Flag = 1;
		PICON = Trig_Port;
	}
	else
		return 1;
	
	switch(Trig_Mode)
	{
		case FALLING_EDGE_TRIG:
			PINEN = Trig_Pin;
			if(Trig_Pin & PIN_0)
				set_PICON_PIT0;
			if(Trig_Pin & PIN_1)
				set_PICON_PIT1;
			if(Trig_Pin & PIN_2)
				set_PICON_PIT2;
			if(Trig_Pin & PIN_3)
				set_PICON_PIT3;
			if(Trig_Pin & PIN_45)
				set_PICON_PIT45;
			if(Trig_Pin & PIN_67)
				set_PICON_PIT67;
			break;
			
		case RISING_EDGE_TRIG:
			PIPEN = Trig_Pin;
			if(Trig_Pin & PIN_0)
				set_PICON_PIT0;
			if(Trig_Pin & PIN_1)
				set_PICON_PIT1;
			if(Trig_Pin & PIN_2)
				set_PICON_PIT2;
			if(Trig_Pin & PIN_3)
				set_PICON_PIT3;
			if(Trig_Pin & PIN_45)
				set_PICON_PIT45;
			if(Trig_Pin & PIN_67)
				set_PICON_PIT67;
			break;
			
/*		case LOW_LEVEL_TRIG:
			PINEN = Trig_Pin;
			if(Trig_Pin & PIN_0)
				clr_PICON_PIT0;
			if(Trig_Pin & PIN_1)
				clr_PICON_PIT1;
			if(Trig_Pin & PIN_2)
				clr_PICON_PIT2;
			if(Trig_Pin & PIN_3)
				clr_PICON_PIT3;
			if(Trig_Pin & PIN_45)
				clr_PICON_PIT45;
			if(Trig_Pin & PIN_67)
				clr_PICON_PIT67;
			break;

		case HIGH_LEVEL_TRIG:
			PIPEN = Trig_Pin;
			if(Trig_Pin & PIN_0)
				clr_PICON_PIT0;
			if(Trig_Pin & PIN_1)
				clr_PICON_PIT1;
			if(Trig_Pin & PIN_2)
				clr_PICON_PIT2;
			if(Trig_Pin & PIN_3)
				clr_PICON_PIT3;
			if(Trig_Pin & PIN_45)
				clr_PICON_PIT45;
			if(Trig_Pin & PIN_67)
				clr_PICON_PIT67;
			break;
*/
		default:
			break;
	}

	return 0;
}

/************************************************
*    TIMER 0 interrupt Initial setting
*@param Cycle TIMER0 count period value in us
*@return setting result
*************************************************/
u8 Timer0_Init(u16 Cycle)
{
	ENABLE_TIMER0_MODE1;	//Timer0 operate in mode1  16-bits counter
	if(Cycle < 4000)		//if Timer0 cycle less than 4ms,cycle = (0xFFFF-TH0<<8-TL0)/16MHz
	{
		TIMER0_FSYS;
		//TL0 = (u8)(0xFFFF -Cycle*16);
		//TH0 = (0xFFFF -Cycle*16) >> 8;
		switch(Cycle)
		{
			case 200:	// 200us
				TL0 = Timer0_TL0_Init_200us;
				TH0 = Timer0_TH0_Init_200us;
				break;
			case 400:	// 400us
				TL0 = Timer0_TL0_Init_400us;
				TH0 = Timer0_TH0_Init_400us;
				break;
			default:
				break;
		}
	}
	else if(Cycle < 45000)	//if Timer0 cycle less than 45ms,cycle = (0xFFFF-TH0<<8-TL0)/16MHz/12
	{
		TIMER0_FSYS_DIV12;
		//TL0 = (u8)(0xFFFF -Cycle*16/12);
		//TH0 = (0xFFFF -Cycle*16/12) >> 8;
		switch(Cycle)
		{
			default:
				break;
		}
	}
	else
		return 1;
	
	ENABLE_TIMER0_INTERRUPT;               //enable Timer0 interrupt
    	set_TCON_TR0;                                  //Timer0 run	
    	
	return 0;
}

/************************************************
*    TIMER 3 interrupt Initial setting
*@param Cycle TIMER3 count period value in ms
*@return setting result
*************************************************/
u8 Timer3_Init(u16 Cycle)
{
	if(Cycle < 50)		//if Timer3 cycle less than 50ms
	{
		T3CON |= Timer3_DIV16;	//Timer3 clock is 16Mhz/16
		RL3	= (u8)(Cycle*1000);	
		RH3	= (Cycle*1000)>>8;
	}
	else if(Cycle < 500)	//if Timer3 cycle less than 500ms
	{
		T3CON |= Timer3_DIV128;	//Timer3 clock is 16Mhz/128
		RL3	= (u8)(Cycle*125);
		RH3	= (Cycle*125)>>8;
	}
	else
		return 1;
	
	EIE1 |= Timer3_EnableInterrupt;		//enable Timer3 interrupt
	T3CON |= Timer3_Start;			//turn on Timer3 count
	
	return 0;
}
/************************************************
*    UART  interrupt Initial setting
*@param u8UartPort UART peripheral select and TIMER
*@param u32Baudrate UART peripheral communication speed
*@return setting result
*************************************************/
u8 Uart_Open(u8 u8UartPort,u32 u32Baudrate)
{
	switch(u8UartPort)
	{
		case UART0_Timer1:
			P06_PUSHPULL_MODE;    	//Setting UART0 pin as Quasi mode for transmit
			P07_INPUT_MODE;    	//Setting UART0 pin as Input mode for receive 
			SCON = 0x50;       		//UART0 Mode1,REN=1,TI=1
			TMOD |= 0x20;     		//Timer1 Mode1
			PCON |= 0x80;          	//UART0 Double Rate Enable
			CKCON |= 0x10;		//Timer 1 as clock source
			T3CON &= 0xDF;          //Serial port 0 baud rate clock source = Timer1
			TH1 = 256 - (16000000/16/u32Baudrate);
			TCON |= 0x40;
			IE |= Uart0_EnableInterrupt;
			break;
			
		case UART0_Timer3:
			P06_PUSHPULL_MODE;  	//Setting UART1 pin as Quasi mode for transmit
			P07_INPUT_MODE; 	 	//Setting UART1 pin as Input mode for receive 
			SCON = 0x50;     	//UART1 Mode1,REN_1=1,TI_1=1
			PCON |= 0x80;          	//UART0 Double Rate Enable
			T3CON &= 0xF8;   	//T3PS2=0,T3PS1=0,T3PS0=0(Prescale=1)
			T3CON|= 0x20;		//UART0 baud rate clock source = Timer3
			RH3    = (65536 - (16000000/16/u32Baudrate))>>8;  
			RL3    = (65536 - (16000000/16/u32Baudrate));     
			T3CON |= 0x08;             //Trigger Timer3   
			IE |= Uart0_EnableInterrupt;
			break;

		case UART1_Timer3:
			P16_PUSHPULL_MODE;  	//Setting UART1 pin as Quasi mode for transmit
			P02_INPUT_MODE; 	 	//Setting UART1 pin as Input mode for receive 
			SCON_1 = 0x50;     	//UART1 Mode1,REN_1=1,TI_1=1
			T3CON = 0x80;     		//T3PS2=0,T3PS1=0,T3PS0=0(Prescale=1), UART1 in MODE 1
			RH3    = (65536 - (16000000/16/u32Baudrate))>>8;  
			RL3    = (65536 - (16000000/16/u32Baudrate));     
			T3CON |= 0x08;             //Trigger Timer3   
			EIE1 |= Uart1_EnableInterrupt;
			break;
		default:
			return 1;
	}
	return 0;
}

/************************************************
*    GPIO Initial setting
*************************************************/
void Gpio_Init(void)
{
	//-----Input------//
	P15_INPUT_MODE;
	//-----Output------//
	P00_PUSHPULL_MODE;
	P01_PUSHPULL_MODE;
	P03_PUSHPULL_MODE;
	P04_PUSHPULL_MODE;
	P05_PUSHPULL_MODE;
	P07_PUSHPULL_MODE;

	P10_PUSHPULL_MODE;
	P11_PUSHPULL_MODE;
	P12_PUSHPULL_MODE;
	P13_PUSHPULL_MODE;
	P14_PUSHPULL_MODE;
	P17_PUSHPULL_MODE;

	P30_PUSHPULL_MODE;
}

/************************************************
*    ADC Initial setting
*************************************************/
void ADC_Init(void)
{
	ENABLE_ADC_AIN3;
	ADCCON1|=0X30;            /* clock divider */
	ADCCON2|=0X0E;            /* AQT time */
	AUXR1|=SET_BIT4;          /* ADC clock low speed */
}
